package com.qf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sym
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/6/21 18:51
 */
@RestController
@RequestMapping("/brand")
@Api(value = "品牌制造商：列表查询、添加、删除、修改")
//解决前后端跨域访问问题
@CrossOrigin("*")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private  CategoryService categoryService;

//    @Autowired
//    private CategoryService categoryService;


    /**
     * 品牌制造商列表
     * @param id    根据品牌商id查询
     * @param name  根据品牌商名字查询
     * @param page  当前页，默认1
     * @param limit 每页显示多少条，默认20条
     * @param sort  排序字段名，默认按照add_time添加时间排序
     * @param order 排序方式，默认desc降序
     * @return
     */
    @GetMapping("/list")
    public Object brandList(Integer id , String name ,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer limit,
                            @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){

        //调用service业务方法
        IPage<DtsBrand> resultPage = brandService.queryBrandPage(id, name, page, limit, sort, order);

        //获取查询的数据
        List<DtsBrand> list = resultPage.getRecords();
        //获取数据总条数
        long total = resultPage.getTotal();

        //封装数据并返回数据
        Map<String , Object> data = new HashMap<>();
        data.put("total",total);
        data.put("items",list);

        return ResponseUtil.ok(data);

    }


    /**
     * 修改品牌信息
     * @return
     */
    @RequestMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        int count = brandService.update(brand);
        if (count == 0) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @RequestMapping("/create")
    public Object add(@RequestBody DtsBrand brand){
        brandService.add(brand);

        return ResponseUtil.ok(brand);
    }

    /**
     * 删除
     * @param brand
     * @return
     */
    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        brandService.delete(brand);

        return ResponseUtil.ok();
    }

    /**
     *品牌制造商下拉
     * @param
     * @return
     */
    @RequestMapping("/catAndAdmin")
    public  Object catAndAdmin(){
        List<CatVo> adminList = adminService.findCatVoAdmin();
        List<CatVo> catVoList = categoryService.findCataVoCat();


        //封装数据并返回数据
        Map<String , Object> data = new HashMap<>();
        data.put("adminList",adminList);
        data.put("categoryList",catVoList);

        return ResponseUtil.ok(data);

    }

}
